<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 *  User_group Model Class
 *
 */
class Counter_model extends CI_Model {

    var $ip;
    var $ses_id;
    var $host;
    var $table;
    var $counter;
    var $use_db;
    var $visitor;

    function __construct() {
        parent::__construct();
        $this->ip = getenv("REMOTE_ADDR");
        $this->host = gethostbyaddr($this->ip);
        $this->ses_id = session_id();
        $this->use_db = 1;
    }

    function is_new_visitor() {
        //$now = $this->config->item('offset') * 3600 + time() - $this->config->item('block_time');
        $now = time();
        if ($this->use_db) {
            //$this->db->query("DELETE FROM " . $this->config->item('visitors') . " WHERE $now > time");
            $sql = "DELETE FROM count_visitors WHERE $now - time > 1800";  // 1800 = 30m
            $this->db->query($sql);


            $sql = "SELECT * FROM count_visitors WHERE ip = '$this->ip'";
            $query = $this->db->query($sql);
            /* if($result = $query->result()){
              $counter['visits_today'] = $result[0]->visitors;
              } */

            //$this->db->fetch_array($this->db->query("SELECT * FROM ".$this->config->item('visitors')." WHERE ip = '$this->ip'"));
            if ($result = $query->result()) {
                $now = $this->config->item('offset') * 3600 + time();
                $this->db->query("UPDATE count_visitors SET time='$now' WHERE ip = '$this->ip'");
                return false;
            } else {
                return true;
            }
        }
    }

    function update_counter() {
        $now = time() + $this->config->item('offset') * 3600;
        if ($this->use_db) {
            $sql = "INSERT INTO count_visitors (time,ip,host) VALUES ('$now','$this->ip','$this->host')";
            $this->db->query($sql);
            $sql = "UPDATE count_total set total=total+1";
            $this->db->query($sql);
            if ($this->db->affected_rows() == 0) { // $this->db->affected_rows()
                $this->db->query("INSERT INTO count_total SET total='1', installtime = UNIX_TIMESTAMP()");
            }
            $this->db->query("UPDATE count_daily SET visitors=visitors+1 WHERE day = CURDATE()");
            if ($this->db->affected_rows() == 0) {
                $this->db->query("INSERT INTO count_daily SET visitors='1', day = CURDATE()");
            }
        }
    }


        function show_counter() {
        $counter = array();
        if ($this->use_db) {
            if ($this->is_new_visitor()) {
                $this->update_counter();
            }

            $sql = "SELECT COUNT(*) as num_online FROM count_visitors";
            $query = $this->db->query($sql);
            if ($result = $query->result()) {
                $counter['online'] = $result[0]->num_online;
            }

            $sql = "SELECT total FROM count_total";
            $query = $this->db->query($sql);
            if ($result = $query->result()) {
                $counter['total'] = $result[0]->total;
            }
            //$counter['total'] = $this->db->record['total'];
            //$this->db->fetch_array($this->db->query("SELECT visitors FROM ".$this->config->item('daily')." WHERE day = CURDATE()"));
            $sql = "SELECT visitors FROM count_daily WHERE day = CURDATE()";
            $query = $this->db->query($sql);
            if ($result = $query->result()) {
                $counter['visits_today'] = $result[0]->visitors;
            }
            //$counter['visits_today'] = $this->db->record['visitors'];
            //$this->db->close_db();
        } /* else {
          if ($this->is_new_visitor()) {
          $this->update_counter();
          } else {
          $fp = fopen($this->config['counter'], "r");
          flock($fp, 2);
          $this->counter = fgets($fp, 4096);
          flock($fp, 3);
          fclose($fp);
          $fp = fopen($this->config['daylog'], "r");
          flock($fp, 2);
          $first_row = fgets($fp, 4096);
          $this->visitor = chop(fgets($fp, 4096));
          flock($fp, 3);
          fclose($fp);
          }
          $counter['total'] = $this->counter;
          $counter['visits_today'] = $this->visitor;
          } */
        return $counter;
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    function insert($data) {

        $sql = "INSERT INTO `gtb_transport` " .
                "(" .
                "`vi_name`, `en_name`, `active`, `position`)" .
                " VALUES ";

        $sql .= "(" .
                $this->db->escape($data['vi_name']) . ", " .
                $this->db->escape($data['en_name']) . ", " .
                intval($data['active']) . ", " .
                intval($data['position']) .
                ")";

        $this->db->query($sql);
        return $this->db->insert_id();
    }

    function update($data) {
        $sql = "UPDATE gtb_transport
			SET " .
                " vi_name = " . $this->db->escape($data['vi_name']) . "," .
                " en_name = " . $this->db->escape($data['en_name']) . "," .
                " active = " . $this->db->escape($data['active']) . "," .
                " position = " . intval($data['position']) .
                " WHERE id = " . intval($data['id']);
        $this->db->query($sql);
        return $this->db->affected_rows();
    }

    function delete($id) {
        // Kiem tra trc khi xoa, neu co record tham chieu thi ko cho xoa.
        // .......... chua lam

        if (!is_array($id))
            $id = array($id);
        $ids = implode(',', $id);
        $sql = "DELETE FROM gtb_transport WHERE id IN ($ids)";
        //$sql = "DELETE FROM gtb_pcategory WHERE id_pcategory = " . intval($id);
        $this->db->query($sql);
        return $this->db->affected_rows();
    }

    function getAll() {
        $result = null;
        $sql = "SELECT * " .
                " FROM gtb_transport " .
                " ORDER BY id DESC";

        $query = $this->db->query($sql);
        $result = $query->result_array();

        return $result;
    }

    function getAllCount() {
        $result = null;
        $sql = "SELECT COUNT(*) as num_record " .
                " FROM gtb_transport ";

        $query = $this->db->query($sql);
        $result = $query->result();

        if ($result)
            return $result[0]->num_record;

        return 0;
    }

    function getMaxPosition() {

        $sql = "SELECT MAX(position) as position_max
				FROM gtb_transport
                        ";
        $query = $this->db->query($sql);
        if ($result = $query->result()) {
            return $result[0]->position_max;
        }

        return 0;
    }

    function getById($id) {

        $sql = "SELECT *
				FROM gtb_transport
				WHERE id = " . intval($id);
        $query = $this->db->query($sql);
        if ($result = $query->result_array()) {
            return $result[0];
        }

        return null;
    }

}

// EOF

